<template>
	<view class="register">

		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体 -->
			<view class="main">
				<wInput v-model="tbUser.phone" type="text" maxlength="11" placeholder="手机号"></wInput>
				<wInput v-model="tbUser.password" type="password" maxlength="11" placeholder="登录密码" isShowPass></wInput>
				<wInput v-model="tbUser.code" type="number" maxlength="4" placeholder="验证码" isShowCode ref="runCode"
					@tap="getVerCode()"></wInput>

			</view>

			<wButton class="wbutton" text="注 册" :rotate="isRotate" @click.native="startReg()"></wButton>
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	import {Toast} from '../../common/common.js'
	import {
		sendCode,
		register
	} from '@/api/user.js'
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData: '', // 用户/电话
				passData: '', //密码
				verCode: "", //验证码
				showAgree: true, //协议是否选择
				isRotate: false, //是否加载旋转
				tbUser: {
					password: '', //密码
					phone: '', // 用户/电话
					code: "" //验证码
				},
			}
		},
		components: {
			wInput,
			wButton,
		},
		mounted() {
			_this = this;
		},
		methods: {
			isShowAgree() {
				//是否选择协议
				_this.showAgree = !_this.showAgree;
			},
			getVerCode() {
				//获取验证码
				if (_this.tbUser.phone.length != 11) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '手机号不正确'
					});
					return false;
				}
				sendCode(this.tbUser).then(res => {
					if (res.data.code == 20000) {
						this.$refs.runCode.$emit('runCode'); //触发倒计时（一般用于请求成功验证码后调用）
						Toast({
							"title": res.data.message
						})
					} else {
						Toast({
							"title": res.data.message
						})
					}
				})
				setTimeout(function() {
					_this.$refs.runCode.$emit('runCode', 0); //假装模拟下需要 终止倒计时
				}, 60000)
			},
			startReg() {
				//注册
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.tbUser.phone.length != 11) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '手机号不正确'
					});
					return false;
				}
				if (this.tbUser.password.length < 6) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码不能小于6位'
					});
					return false;
				}
				if (this.tbUser.code.length != 4) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '验证码不正确'
					});
					return false;
				}
				register(this.tbUser).then(res=>{
					if(res.data.code==20000){
						uni.navigateTo({
							url:'./login'
						})
						Toast({title:res.data.message})
					}else{
						Toast({title:res.data.message})
					}
				})
				
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
